package ICM.client;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import common.CommandMessage;
import common.TextMessage;

import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Scrollbar;
import javax.swing.JTextPane;

public class SupervisorUI extends JPanel implements ActionListener,Observer {

	
	private Client c;
	private ClientUI ui;
	private SystemEngineerUI SE;
	private JButton btnSendEmail;
	private JButton btnBack;
	private JLabel lblNewLabel;
	private JLabel lblYourEmailText;
	private boolean EmailSent;
	private boolean CloseRequest;
	private JTextPane Message;
	private JButton btnCloseRequest;
	
	public SupervisorUI(final Client c,final ClientUI ui){
		this.c=c;
		this.ui=ui;
		setLayout(null);
		c.addObserver(this);
		
		
		
		JLabel lblClosing = new JLabel("Closing");
		lblClosing.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblClosing.setBounds(236, 11, 121, 35);
		add(lblClosing);
		
		JLabel lblYourRequestid = new JLabel("Your RequestID:");
		lblYourRequestid.setBounds(43, 53, 102, 14);
		add(lblYourRequestid);
		
		lblNewLabel = new JLabel(SE.info[0]);
		lblNewLabel.setBounds(145, 53, 77, 14);
		add(lblNewLabel);
		
		btnSendEmail = new JButton("Send email");
		btnSendEmail.addActionListener(this);
		btnSendEmail.setBounds(310, 238, 112, 23);
		add(btnSendEmail);
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(this);
		btnBack.setBounds(56, 238, 89, 23);
		add(btnBack);
		
		lblYourEmailText = new JLabel("Your Email Text");
		lblYourEmailText.setBounds(43, 110, 102, 14);
		add(lblYourEmailText);
		
		Message = new JTextPane();
		Message.setBounds(157, 92, 200, 93);
		add(Message);
		
		btnCloseRequest = new JButton("Close request");
		btnCloseRequest.addActionListener(this);
		btnCloseRequest.setBounds(441, 126, 118, 35);
		add(btnCloseRequest);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		if (e.getSource().equals(btnSendEmail)) 
		{
			String mess=Message.getText().trim();
			
			CommandMessage msg = new CommandMessage("Send email",new String[] {mess,ui.getUser().getEmail()});
			try {
				c.sendToServer(msg);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
			    Thread.sleep(800);
			} catch(InterruptedException ex) {
			    Thread.currentThread().interrupt();
			}
			
			if (EmailSent)
				ui.ShowMessageBox("Information", "Email was sent" ,JOptionPane.INFORMATION_MESSAGE);
			else
				ui.ShowMessageBox("Error", "Can't send email" ,JOptionPane.ERROR_MESSAGE);
			
		}
		else if (e.getSource().equals(btnCloseRequest)) 
		{
			CommandMessage msg = new CommandMessage("Close request",new String[] {SE.info[0],SE.info[2]});
			try {
				c.sendToServer(msg);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
			    Thread.sleep(800);
			} catch(InterruptedException ex) {
			    Thread.currentThread().interrupt();
			}
			
			if (CloseRequest)
				ui.ShowMessageBox("Information", "Request was closed" ,JOptionPane.INFORMATION_MESSAGE);
			else
				ui.ShowMessageBox("Error", "Can't close request" ,JOptionPane.ERROR_MESSAGE);
			
		}
		else if(e.getSource().equals(btnBack)){
			try 
			{ui.SetContent(ClientUI.SystemEngineerWindow_Screen);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		String msg = "";
		String cmd = "";
		if (arg1 instanceof TextMessage)
			msg = ((TextMessage)arg1).ToDebugMessage();
		else if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if(ms.getCommand().equals("Email sent")){
				EmailSent = Boolean.parseBoolean(ms.getParams()[1].toString());
			}
			else if(ms.getCommand().equals("Request closed")){
				CloseRequest = Boolean.parseBoolean(ms.getParams()[1].toString());
			}
		}
		else
			msg = arg1.toString();
	}
}
